<template>
	<view>
		群聊示例(群id：laoxiang)
		<!--server websocket服务器 channel订阅的频道（加入的群聊） -->
	</view>
</template>

<script>
import { mywebsocket } from '@/common/mywebsocket.js';
export default {
	data() {
		return { channel: '', online: 0, websocket: null };
	},
	onShow() {
		var that = this;
		var eventHanders = [
			{
				event: 'online',
				hander: function(res) {
					that.online < res && (that.online = res);
				}
			}
		];
		var msgHanders = [
			{
				cmd: 'hello',
				hander: function(msg) {
					that.online = msg.data;
				}
			},
			{
				cmd: 'bye',
				hander: function(msg) {
					that.online = msg.data;
				}
			},
			{
				cmd: 'msg',
				hander: function(msg) {
					//that.$refs.barrage.add(msg.data);
				}
			}
		];
		this.websocket = mywebsocket(this.channel, msgHanders, eventHanders);
	},
	onUnload() {
		this.websocket && this.websocket.leaveChan(this.channel);
	},
	methods: {
		sendmsg(msg) {
			if (!msg) return;
			var pic = this.loginInfo.hasLogin ? this.imgServer + this.loginInfo.user.avatar : '';
			// console.error(pic)
			//pic="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3256100974,305075936&fm=26&gp=0.jpg"
			var msg1 = {
				pic: pic,
				text: msg
			};
			//console.log(msg1)
			var msg = this.websocket.getCmd().msg(msg1, this.channel);
			this.websocket.SendChannelMsg(this.channel, msg);
			// this.$refs.barrage.add(msg1);
		}
	}
};
</script>

<style></style>
